//
//  CWWelcomeViewController.swift
//  新浪微博练习02
//
//  Created by 曹魏 on 16/10/4.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage
private let bottomMargin : CGFloat = 120
class CWWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置背景颜色
        self.view.backgroundColor  = UIColor.white
        setUpUI()
        
        
    }
    //执行动画需要放在视图已经显示的生命周期方法
    override func viewDidAppear(_ animated: Bool) {
        
        //执行动画
        startAnimation()
    }
    //开始动画
    
    func startAnimation() {
        let offsetY = -(UIScreen.main.bounds.height - bottomMargin - 85)
        self.iconView.snp.updateConstraints ({ (make) in
            make.bottom.equalTo(self.view).offset(offsetY)
        })
        UIView.animate(withDuration: 1.2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: { 
            self.view.layoutIfNeeded()
            }) { (_) in
                UIView.animate(withDuration: 0.5, animations: { 
                    self.welcomeLabel.alpha = 1
                    
                    }, completion: { (_) in
//                        print("ok")
                        //切换到TabbarController modal  切换到tabbarcontroller
                        NotificationCenter.default.post(name: NSNotification.Name(KchangeRootViewController), object: nil)
                })
        }
    }
    
    
    
    
    //设置UI 界面
    private func setUpUI() {
        //添加子控件
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        //设置约束
        iconView.snp.makeConstraints { (make) in
            make.bottom.equalTo(view.snp.bottom).offset(-bottomMargin)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        welcomeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp.bottom).offset(20)
            make.centerX.equalTo(iconView)
            
        }
        //设置图片的url地址
        //setImageWith AFNetworking的方法  AFN只是内存缓存图片
        //        let urlString = HMUserAccountViewModel.sharedAccountViewModel.userAccount?.avatar_large ?? ""
        //        let url = URL(string: urlString)
        //        //URL 隐式的可选类型 实际上还是一个可选类型 强制解包 接收的类型是必选类型才需要强制解包
        //        iconView.sd_setImage(with: url)\
        iconView.sd_setImage(with: CWUserAccountViewModel.sharedAccountViewModel.iconURL)
        
        
    }
    //懒加载控件
    private lazy var iconView : UIImageView = {
        let iv = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
        iv.cornerRadius = 42
        //设置边线
        iv.layer.borderWidth = 1
        //设置边线颜色
        iv.layer.borderColor = UIColor.orange.cgColor
        
        return iv
        
    }()
    private lazy var welcomeLabel : UILabel = {
        let l = UILabel(title: "欢迎回来", textColor: UIColor.darkGray, fontSize: 16)
        l.alpha = 0
        return l
        
    }()

}
